home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Application / Examples / Harvest MiniEdit / mini.file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-24  |  5.3 KB  |  274 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.file.c
  4.     
  5.     file functions for Miniedit
  6.     
  7. *********************************************************************/
  8.     
  9. #include <TextEdit.h>
  10. #include <Errors.h>
  11. #include <Files.h>
  12. #include <Windows.h>
  13. #include <StandardFile.h>
  14. #include "MiniEdit.h"
  15. #include "mini.file.h"
  16. #include "mini.windows.h"
  17. #include "mini.print.h"
  18.  
  19. Str255         theFileName;
  20. static short    theVRefNum;
  21.  
  22. extern TEHandle  TEH;
  23. extern WindowPtr myWindow;
  24. extern char         dirty;
  25.  
  26.  
  27.     /**
  28.      **        Prototypes for private functions.
  29.      **        (They really should be static.)
  30.      **
  31.      **/
  32.  
  33. int pStrCopy(StringPtr a, StringPtr b);
  34. int OldFile (Str255 fn, short *vRef);
  35. int ReadFile (short refNum, TEHandle textH);
  36. int FileError(Str255 s, Str255 f);
  37. int SaveAs (Str255 fn, short *vRef);
  38. int SaveFile (Str255 fn, short vrn);
  39. int NewFile (Str255 fn, short *vRef);
  40. int OldFile (Str255 fn, short *vRef);
  41. int CreateFile (Str255 fn, short *vRef, short *theRef);
  42. int WriteFile (short refNum, char *p, long num);
  43.  
  44.  
  45.  
  46. int SetUpFiles(void)
  47. {
  48.     pStrCopy("\p", theFileName);
  49.     theVRefNum = 0;
  50. }
  51.  
  52. int DoFile (int item)
  53.  
  54. {
  55.     short     vRef, refNum, io;
  56.     Str255    fn;
  57.  
  58.     switch (item) {
  59.         case fmNew: 
  60.             SetWTitle(myWindow, "\pUntitled");
  61.             ShowWindow(myWindow);
  62.             dirty = 0;
  63.             break;
  64.  
  65.         case fmOpen:
  66.             if (OldFile(fn, &vRef ))
  67.                 if (FSOpen(fn, vRef, &refNum)==noErr) {
  68.                     if (ReadFile(refNum, TEH)) {
  69.                         pStrCopy(fn, theFileName);
  70.                         theVRefNum = vRef;
  71.                         SetWTitle(myWindow, theFileName);
  72.                         dirty = 0;
  73.                     }
  74.                     if (FSClose(refNum)==noErr) ;
  75.                     ShowWindow(myWindow);
  76.                     TESetSelect(0, 0, TEH);
  77.                     ShowSelect();
  78.                 }
  79.                 else FileError("\pError opening ", fn);
  80.             break;
  81.  
  82.         case fmClose:
  83.             if (dirty) {
  84.                 ParamText("\pSave changes for ā€œ",
  85.                         (theFileName[0]==0) ? "\pUntitled" : theFileName,
  86.                         "\pā€?", "\p");
  87.                 switch (Alert(AdviseAlert, 0L)) {
  88.                 case aaSave:
  89.                     if (theFileName[0]==0) {
  90.                         fn[0] = 0;
  91.                         if (!SaveAs(fn, &vRef)) return(0);
  92.                     }
  93.                      else if (!SaveFile(theFileName, theVRefNum)) return(0);
  94.                      break;
  95.                  case aaCancel: return(0);
  96.                  case aaDiscard: dirty = 0;
  97.                  }
  98.              }
  99.             CloseMyWindow();
  100.             break;
  101.         case fmSave:
  102.             if (theFileName[0]==0) goto saveas;
  103.             SaveFile(theFileName, theVRefNum);
  104.             break;
  105.         case fmSaveAs:
  106.     saveas:
  107.             fn[0] = 0;
  108.             if (SaveAs(fn, &vRef )) {
  109.                 pStrCopy(fn, theFileName);
  110.                 theVRefNum = vRef;
  111.                 SetWTitle(myWindow, theFileName);
  112.             }
  113.             break;
  114.         case fmRevert:
  115.             ParamText("\pRevert to last saved version of ā€œ",
  116.                     theFileName, "\pā€?", "\p");
  117.             switch (Alert(AdviseAlert, 0L)) {
  118.             case aaSave:
  119.                 HidePen();
  120.                 TESetSelect(0, (**TEH).teLength, TEH);
  121.                 ShowPen();
  122.                 TEDelete(TEH);
  123.                 if ((theFileName[0]!=0) &&
  124.                     (FSOpen(theFileName, theVRefNum, &refNum)==noErr)) {
  125.                     dirty = !ReadFile(refNum, TEH); 
  126.                     /* I don't check for bad read! */
  127.                     if (FSClose(refNum)==noErr) ;
  128.                 }
  129.                 ShowWindow(myWindow);
  130.                 UpdateWindow(myWindow);
  131.              case aaCancel:
  132.              case aaDiscard: return(0);;
  133.              }
  134.     
  135.             break;
  136.         case fmPageSetUp:
  137.             DoPageSetUp();
  138.             break;
  139.         case fmPrint:
  140.             PrintText( (**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
  141.                             StringWidth("\pmmmm"));
  142.             break;
  143.         case fmQuit: 
  144.             if (DoFile(fmClose))
  145.                 ExitToShell();
  146.     }
  147.     return(1);
  148. }
  149.  
  150. static Point SFGwhere = { 90, 82 };
  151. static Point SFPwhere = { 106, 104 };
  152. static SFReply reply;
  153.  
  154. int SaveAs (Str255 fn, short  *vRef)
  155.  
  156. {
  157.     short refNum;
  158.     
  159.     if (NewFile(fn, vRef)) 
  160.         if (!CreateFile(fn, vRef, &refNum)) {
  161.             FileError("\pError creating file ", fn);
  162.             return (0);
  163.         } else {
  164.             WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  165.             FSClose(refNum);
  166.             dirty = 0;
  167.             return(1);
  168.         }
  169. }
  170.  
  171. int SaveFile (Str255 fn, short vrn)
  172.  
  173. {
  174.     short refNum;
  175.     
  176.     if (FSOpen(fn, vrn, &refNum) != noErr) {
  177.         FileError("\pError opening file ", fn);
  178.         return (1);
  179.     } else {
  180.         WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  181.         dirty = 0;
  182.         FSClose(refNum);
  183.         return(1);
  184.     }
  185. }
  186.  
  187. int NewFile (Str255 fn, short *vRef)
  188.  
  189. {
  190.     SFPutFile(SFPwhere, "\pSave file as", fn, 0L, &reply);
  191.     if (!reply.good)
  192.         return (0);
  193.     else {
  194.         pStrCopy(reply.fName, fn);
  195.         *vRef = reply.vRefNum;
  196.         return(1);
  197.     }
  198. }
  199.  
  200. int OldFile (Str255 fn, short *vRef)
  201.  
  202. {
  203.     SFTypeList    myTypes;
  204.     
  205.     myTypes[0]='TEXT';
  206.  
  207.     SFGetFile(SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply );
  208.  
  209.     if (!reply.good)
  210.         return (0);
  211.     else {
  212.         pStrCopy(reply.fName, fn);
  213.         *vRef = reply.vRefNum;
  214.         return(1);
  215.     }
  216. }
  217.  
  218. int CreateFile (Str255 fn, short *vRef, short *theRef)
  219.  
  220. {
  221.     int io;
  222.     
  223.     io=Create(fn, *vRef, 'CEM8', 'TEXT');
  224.     if ((io == noErr) || (io == dupFNErr))
  225.         io = FSOpen(fn, *vRef, theRef );
  226.  
  227.     return ((io == noErr) || (io == dupFNErr));
  228. }
  229.  
  230. int WriteFile (short refNum, char *p, long num)
  231.  
  232. {
  233.     short io;            /*     a real application would want to check 
  234.                         this return code for failures */
  235.     
  236.     io=FSWrite(refNum, &num, p);
  237. }
  238.  
  239. int ReadFile (short refNum, TEHandle textH)
  240.  
  241. {
  242.     char    buffer[256];
  243.     long    count;
  244.     short        io;
  245.     
  246.     TESetSelect(0, (**textH).teLength, textH);
  247.     TEDelete(textH);
  248.     do {
  249.         count = 256;
  250.         io = FSRead(refNum, &count, &buffer);
  251.         TEInsert(&buffer, count, textH);
  252.     } while (io==noErr);
  253.     
  254.     return (io == eofErr);
  255. }
  256.  
  257. int pStrCopy (StringPtr p1, StringPtr p2)
  258.  
  259. /* copies a pascal string from p1 to p2 */
  260. {
  261.     register int len;
  262.     
  263.     len = *p2++ = *p1++;
  264.     while (--len>=0) *p2++=*p1++;
  265. }
  266.  
  267.  
  268. int FileError(Str255 s, Str255 f)
  269.  
  270. {
  271.     ParamText(s, f,"\p", "\p");
  272.     Alert(ErrorAlert, 0L);
  273. }
  274.